//
//  CommunityViewController.m
//  FreeTravel
//
//  Created by 徐康 on 16/8/26.
//  Copyright © 2016年 KX. All rights reserved.
#import "FreeTravelNetwork.h"
#import "FreeTravelMacro.h"
#import "CommunityViewController.h"
#import "Masonry.h"
#import "MJRefresh.h"
#import "HotCommunity.h"
#import "AppearanceCommunity.h"
#import "CompanionsCommunity.h"
@interface CommunityViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UIButton *hotButton;
@property (nonatomic, strong) UIButton *appearanceButton;
@property (nonatomic, strong) UIButton *companionsButton;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIView *whiteSpots;
@end

@implementation CommunityViewController

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.hotButton.hidden = YES;
    self.appearanceButton.hidden = YES;
    self.companionsButton.hidden = YES;
    self.whiteSpots.hidden = YES;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.hotButton.hidden = NO;
    self.appearanceButton.hidden = NO;
    self.companionsButton.hidden = NO;
    self.whiteSpots.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.hotButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.navigationController.navigationBar addSubview:self.hotButton];
    self.hotButton.frame = CGRectMake(SCREEN_WIDTH/4-40, 11, 80, 20);
    [self.hotButton setTitle:@"热议" forState:UIControlStateNormal];
    [self.hotButton addTarget:self action:@selector(goHot) forControlEvents:UIControlEventTouchUpInside];
    
    self.appearanceButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.navigationController.navigationBar addSubview:self.appearanceButton];
    self.appearanceButton.frame = CGRectMake(SCREEN_WIDTH/2-40, 11, 80, 20);
    [self.appearanceButton setTitle:@"进入版面" forState:UIControlStateNormal];
    [self.appearanceButton addTarget:self action:@selector(goAppearance) forControlEvents:UIControlEventTouchUpInside];
    
    self.companionsButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.navigationController.navigationBar addSubview:self.companionsButton];
    self.companionsButton.frame = CGRectMake(SCREEN_WIDTH/4*3-40, 11, 80, 20);
    [self.companionsButton setTitle:@"找旅伴" forState:UIControlStateNormal];
    [self.companionsButton addTarget:self action:@selector(goComanions) forControlEvents:UIControlEventTouchUpInside];
    
    self.whiteSpots = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/4, 33, 4, 4)];
    self.whiteSpots.backgroundColor = [UIColor whiteColor];
    self.whiteSpots.layer.cornerRadius = 2;
    self.whiteSpots.layer.masksToBounds = YES;
    [self.navigationController.navigationBar addSubview:self.whiteSpots];
    
    [self createCollectionView];
}

- (void)createCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT-108);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT-108) collectionViewLayout:layout];
    [self.view addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.pagingEnabled  = YES;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[HotCommunity class] forCellWithReuseIdentifier:@"HotCommunity"];
    [self.collectionView registerClass:[AppearanceCommunity class] forCellWithReuseIdentifier:@"AppearanceCommunity"];
    [self.collectionView registerClass:[CompanionsCommunity class] forCellWithReuseIdentifier:@"CompanionsCommunity"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 3;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        HotCommunity *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HotCommunity" forIndexPath:indexPath];
        
        return cell;
    }else if (indexPath.row == 1){
        AppearanceCommunity *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AppearanceCommunity" forIndexPath:indexPath];
        return cell;
    }else{
        CompanionsCommunity *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CompanionsCommunity" forIndexPath:indexPath];
        return cell;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView == self.collectionView) {
        NSInteger currentItemCount = scrollView.contentOffset.x/SCREEN_WIDTH;
        [UIView animateWithDuration:0.5 animations:^{
            self.whiteSpots.frame = CGRectMake(SCREEN_WIDTH/4 * (currentItemCount+1), 33, 4, 4);
        }];
    }
}
- (void)goHot{
    
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    [UIView animateWithDuration:0.5 animations:^{
        self.whiteSpots.frame = CGRectMake(SCREEN_WIDTH/4, 33, 4, 4);
    }];
}
- (void)goAppearance{
    
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    [UIView animateWithDuration:0.5 animations:^{
        self.whiteSpots.frame = CGRectMake(SCREEN_WIDTH/2, 33, 4, 4);
    }];
}
- (void)goComanions{
    
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:2 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    [UIView animateWithDuration:0.5 animations:^{
        self.whiteSpots.frame = CGRectMake(SCREEN_WIDTH/4*3, 33, 4, 4);
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
